<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>删除订单详细</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #FFEEF7;
            margin: 0;
            padding: 20px;
            color: #333;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        .form-group {
            margin-bottom: 10px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            padding: 10px 20px;
            cursor: pointer;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
        }
        button:hover {
            background-color: #4CAF50;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>删除订单详细</h2>
    <form id="deleteOrderDetailForm">
        <div class="form-group">
            <label for="orderDetailID">订单详细ID:</label>
            <input type="text" id="orderDetailID" name="orderDetailID" readonly>
        </div>
        <div class="form-group">
            <label for="orderDetailOrderID">订单ID:</label>
            <input type="text" id="orderDetailOrderID" name="orderDetailOrderID" readonly>
        </div>
        <div class="form-group">
            <label for="orderDetailProductID">产品ID:</label>
            <input type="text" id="orderDetailProductID" name="orderDetailProductID" readonly>
        </div>
        <div class="form-group">
            <label for="orderDetailQuantity">数量:</label>
            <input type="number" id="orderDetailQuantity" name="orderDetailQuantity" readonly>
        </div>
        <div class="form-group">
            <label for="orderDetailUnitPrice">单价:</label>
            <input type="text" id="orderDetailUnitPrice" name="orderDetailUnitPrice" readonly>
        </div>
        <div class="form-group">
            <label for="orderDetailSubtotal">小计:</label>
            <input type="text" id="orderDetailSubtotal" name="orderDetailSubtotal" readonly>
        </div>
        <div class="form-group">
            <button type="button" onclick="deleteOrderDetail()">确认删除</button>
            <button type="button" onclick="window.history.back()">取消</button>
        </div>
    </form>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const orderDetailId = getParameterByName('id');
        if (orderDetailId) {
            fetchOrderDetailDetails(orderDetailId);
        }
    });

    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
        var results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, ' '));
    }

    function fetchOrderDetailDetails(orderDetailId) {
        fetch(`http://localhost:8080/order-details/${orderDetailId}`)
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(orderDetail => {
                document.getElementById('orderDetailID').value = orderDetail.orderDetailID;
                document.getElementById('orderDetailOrderID').value = orderDetail.orderID;
                document.getElementById('orderDetailProductID').value = orderDetail.productID;
                document.getElementById('orderDetailQuantity').value = orderDetail.quantity;
                document.getElementById('orderDetailUnitPrice').value = orderDetail.unitPrice;
                document.getElementById('orderDetailSubtotal').value = orderDetail.subtotal;
            })
            .catch(error => {
                console.error('Error fetching order detail details:', error);
                alert('获取订单详情失败: ' + error.message);
            });
    }

    function deleteOrderDetail() {
        const orderDetailId = getParameterByName('id'); // 从URL中获取订单详细ID

        fetch(`http://localhost:8080/order-details/${orderDetailId}`, {
            method: 'DELETE',
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                console.log('Success:', data);
                alert('订单详细删除成功');
                window.history.back();
            })
            .catch(error => {
                console.error('Error:', error);
                alert('订单详细删除失败: ' + error.message);
            });
    }
</script>

</body>
</html>